package Utils;

import entity.User;

import java.lang.reflect.Field;

/**
 * @author:wmx 2025-03-19 16:23
 * 1、功能:
 * 2、参数{
 * <p>
 * <p>
 * }
 **/
public class BeanUtil {

    public static void setProperty(Object bean, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {
        Class<?> beanClass = bean.getClass();
        Field declaredField = beanClass.getDeclaredField(propertyName);//获取对应的属性
        declaredField.setAccessible(true);//暴力反射
        declaredField.set(bean,value);//设置值


    }
    public static Object getProperty(Object bean, String propertyName) throws NoSuchFieldException, IllegalAccessException {
        Class<?> beanClass = bean.getClass();
        Field declaredField = beanClass.getDeclaredField(propertyName);//获取对应的属性
        declaredField.setAccessible(true);
        return  declaredField.get(bean);
    }
    public static void copyProperties(Object source,String sourceField, Object target,String targetField) throws NoSuchFieldException, IllegalAccessException {
        Class<?> beanClass = source.getClass();
        Class<?> targetClass = target.getClass();

        Field sourcseFields = beanClass.getDeclaredField(sourceField);//获取对应的属性
        Field targetFields = targetClass.getDeclaredField(targetField);//获取对应的属性

        sourcseFields.setAccessible(true);//暴力反射
        targetFields.setAccessible(true);//暴力反射

        Object propertValue = sourcseFields.get(source);//获取原属性的值

        targetFields.set(target,propertValue);//设置属性的值






        System.out.println(propertValue);


    }

}
